package tk3.project;

import java.io.File;

import tk3.project.uploadservice.Client;
import tk3.project.uploadservice.Picture;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;

public class GPSPictureSender implements LocationListener {

	private static GPSPictureSender instance;

	private double lat;
	private double lon;
	private Client client;

	private boolean gpsBased = false;
	private String stream = "";
	private String password = "";
	private String ip = "";
	private int port = 0;
	private String path = "";
	private int radius;
	private int duration;

	/**
	 * 
	 * @param c - The global client object
	 */
	private GPSPictureSender(Client c) {
		refreshSettings();
		client=c;


	}
	
	/**
	 * Refreshes the values of the Settings class
	 */
	private void refreshSettings() {
		if(Settings.getActiveInstance() != null){
			ip = Settings.getActiveInstance().getIP();
			port = Settings.getActiveInstance().getPort();
			path = Settings.getActiveInstance().getPath();
			stream = Settings.getActiveInstance().getStreamName();
			password = Settings.getActiveInstance().getStreamPassword();
			radius = Settings.getActiveInstance().getRadius();
			duration = Settings.getActiveInstance().getDuration();
			gpsBased=Settings.getActiveInstance().isGpsBased();
		}
	
	}
/**
 * returns the current instance. This getInstance method is only called on first call all others can use the method without arguments
 * @param c - the global client object
 * @return the Instance
 */
	public static GPSPictureSender getInstance(Client c) {
		if (instance == null) {
			instance = new GPSPictureSender(c);
		}

		return instance;
	}
	/**
	 * use this method for all calls except the first
	 * @return the instance
	 */
	public static GPSPictureSender getInstance() {
		return instance;
	}
	
	/**
	 * Is called whenever a picture should be uploaded to the Server
	 * @param path the path of the file to be uploaded
	 */
	public void sendPicture(String path) {
		refreshSettings();
		File file = new File(path);
		if(file.exists()){
			Picture picture = null;
			if (gpsBased) {
				picture = new Picture(file, lat, lon, radius, duration);
			} else {	
				picture = new Picture(file, stream, password, duration);
			}

			client.sendPicture(picture);
		}
		

	}
	/**
	 * Updates the location lon/lat
	 */
	public void onLocationChanged(Location location) {
		lat = location.getLatitude();
		lon = location.getLongitude();

	}

	public void onProviderDisabled(String arg0) {

	}

	public void onProviderEnabled(String arg0) {

	}

	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {

	}
	
	

}
